package com.gwtsz.chart.output.utils;

import java.util.ArrayList;

import www.com.library.app.Logger;

/**
 * Created by reeta.zou on 2016/11/9.
 * 指标基本类，包含指标线数量及指标线集合
 */
public class GTTIndicatorsModel {
    public String mName;//指标名
    public int mMinMaxValid; //
    public double mMin;
    public double mMax;
    public int mNumOfLines;//指标线数量,集合大小
    public ArrayList<GTTLineModel> mLineList = new ArrayList();

    /**定义默认线的颜色*/
    private static int[] DEFAULT_LINE_COLORS = {
        0xFF2865e1,		// 蓝
        0xFFFFA608,		// 黄
        0xFFd916aa,		// 紫
        0xFF62ca4a,		// 绿
        0xFF9020FF,		// 淡紫
        0xFF2A00FF,		// 深蓝
    };

    private int length = DEFAULT_LINE_COLORS.length;

    public void print(){
        Logger.e("GTTIndicatorsModel mName = " + mName  +", " + mMax +", " + mMin +", " + mNumOfLines +" ," + mLineList.size());
    }

    /**
     * 复制数据到原来的对象
     * @param model
     */
    public void copyIndicator(GTTIndicatorsModel model){
        mName = model.mName;
        //ma和pbx指标只取前4项
        if(null != mName && (mName.equals("MA") || mName.equals("PBX"))){
            mNumOfLines = 4;
        }else{
            mNumOfLines = model.mNumOfLines;
        }
        mMax = model.mMax;
        mMin = model.mMin;
        mMinMaxValid = model.mMinMaxValid;
        for (int i = 0 ; i < mNumOfLines; i++){
            GTTLineModel copyLine = model.mLineList.get(i);
            if(null != copyLine){
                //复制数据到本地
                if(mLineList.size() > i){
                    GTTLineModel line = mLineList.get(i);
                    if(null != line){
                        line.mLineData.mLines.addAll(0, copyLine.mLineData.mLines);
                    }
                }else{
                    //设置每根指标线的颜色
                    int position = i%length;
                    copyLine.mColor = DEFAULT_LINE_COLORS[position];
                    mLineList.add(copyLine);
                }
            }

            GTTLineModel lineModel = mLineList.get(i);
            if(null != lineModel){
                lineModel.mLineData.mDiffNum = GTTKDataManager.instance().kLineNumbers - lineModel.mLineData.mLines.size();
                Logger.e("copyIndicator " +lineModel.mName + ", "+ GTTKDataManager.instance().kLineNumbers +", " + lineModel.mLineData.mLines.size() + ", " + lineModel.mLineData.mDiffNum);
            }
        }
    }

    /**
     * 复制最后一根指标数据到原来的对象
     * @param model
     */
    public void copyLastIndicator(boolean isAdd, GTTIndicatorsModel model){
//        Logger.e("copyLastIndicator isAdd " + isAdd);

        if(mName == null || !mName.equals(model.mName)){
            return;
        }

        if(mNumOfLines < 1 || mLineList.size() < 1 || model.mLineList.size() < 1){
            return;
        }
        if (mNumOfLines >= mLineList.size()){
            mNumOfLines = mLineList.size();
        }
        for (int i = 0 ; i < mNumOfLines; i++){
            GTTLineModel copyLine = model.mLineList.get(i);
            if(null != copyLine){
                //复制数据到本地
                if(mLineList.size() > i){
                    GTTLineModel line = mLineList.get(i);
                    int lastPosition = line.mLineData.mLines.size();
                    if(copyLine.mLineData.mLines.size() > 0){
                        if(isAdd){
                            line.mLineData.mLines.clear();
                            line.mLineData.mLines.addAll(copyLine.mLineData.mLines);
                        }else{
                            if(lastPosition>0){
                            line.mLineData.mLines.set(lastPosition - 1, copyLine.mLineData.mLines.get(copyLine.mLineData.mLines.size()-1));
                        }
                        }
                    }
                }else{
                    //设置每根指标线的颜色
                    int position = i%length;
                    copyLine.mColor = DEFAULT_LINE_COLORS[position];
                    mLineList.add(copyLine);
                }
            }
        }
    }
}
